<template>
    <div>
        <el-card class="box-card">
            <el-form :model="form" label-width="120px">
                <el-form-item label="医疗品名">
                    <el-input v-model="form.name" />
                </el-form-item>
                <el-form-item label="医疗分类">
                    <el-select v-model="form.sort" placeholder="请选择药品分类">
                        <el-option v-for="i in store.state.jjy.drugsDrugstypeList" :key="i.id" :label="i.type" :value="i.type" />
                    </el-select>
                </el-form-item>
                <el-form-item label="医品规格">
                    <el-input v-model="form.type" />
                </el-form-item>
                <el-form-item label="生产日期">
                    <el-col :span="11">
                        <el-date-picker v-model="form.starttime" type="date" placeholder="2022-11-04"/>
                    </el-col>
                </el-form-item>
                <el-form-item label="有效日期">
                    <el-col :span="11">
                        <el-date-picker v-model="form.endtime" type="date" placeholder="2023-11-04"/>
                    </el-col>
                </el-form-item>
                <el-form-item label="入库数量">
                    <el-input-number v-model="form.count"  />
                </el-form-item>
                <el-form-item label="医品价格">
                    <el-input-number v-model="form.price"  />
                </el-form-item>
                <el-form-item label="医疗图片">
                    <el-upload class="avatar-uploader" :action="action" :show-file-list="false"
                        :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon">
                            <Plus />
                        </el-icon>
                    </el-upload>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">确定添加</el-button>
                    <el-button>Cancel</el-button>
                </el-form-item>
            </el-form>
        </el-card>

    </div>
</template>

<script setup lang="ts">
import { reactive, ref,onMounted} from 'vue'
import { useStore } from 'vuex';
import { ElMessage } from 'element-plus'
import {useRouter} from 'vue-router'

const store=useStore()
const router=useRouter()
let data=reactive([])
const action = `${import.meta.env.VITE_BASE_URL}/drugs/uploaddrugs`
// do not use same name with ref
const form = reactive({
    // 名字
    name: '哇哈哈',
    //分类
    sort: '养生',
    // 规格
    type: '100ml',
    // 生产日期
    starttime: '2022-11-04',
    //有效日期
    endtime: '2025-11-04',
    //入库数量
    count: 100,
    //价格
    price: 100,
    //图片
    img: '',
})
onMounted(async ()=>{
    // let res=await drugsDrugstype()
    // console.log(res.data.data);
    // data=res.data.data
    // console.log(data);
    store.dispatch('jjy/A_drugsDrugstype')
    
})
const imageUrl = ref('')
//上传图片回显
function handleAvatarSuccess(val) {
    form.img=val.drugs
    if (val.code == 0) {
        imageUrl.value = val.drugs_url
    }
}
//上传失败回显
function beforeAvatarUpload(val) {
    console.log(val);
}
//添加
const onSubmit = async () => {
    let res=await store.dispatch('jjy/A_drugsAddReq',form)
    console.log(res);
    if(res==1){
        ElMessage.success('添加成功了')
    router.push('/warehouse')
    }else{
        ElMessage.info('添加失败')
    }
}
</script>

<style scoped>

</style>